//
//  MailSendTask.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-6-13.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "MailSendTask.h"
#import "WebServiceHelper.h"
#import "StringUtil.h"
#import "GTMBase64.h"

@implementation MailSendTask

-(void)execute
{
    NSString *ValidateData = [self.params objectForKey:@"ValidateData"];
    NSString *url = [self.params objectForKey:@"url"];
    NSString *wsNamespace = [self.params objectForKey:@"namespace"];
    NSString *userguid = [self.params objectForKey:@"userguid"];
    NSString *Subject = [self.params objectForKey:@"Subject"];
    NSString *ToUserGuidList = [self.params objectForKey:@"ToUserGuidList"];
    NSString *SecretUserGuidList = [self.params objectForKey:@"SecretUserGuidList"];
    NSString *ChaoSongUserGuidList = [self.params objectForKey:@"ChaoSongUserGuidList"];
    NSString *MailContent = [self.params objectForKey:@"MailContent"];
    NSMutableArray *ATTACHList = [self.params objectForKey:@"ATTACHList"];
    NSString *IFNotice = [self.params objectForKey:@"IFNotice"];
    NSMutableString *attachXML = [[NSMutableString alloc]initWithString:@""];
    [attachXML appendString:@"<ATTACHList>"];
    for (int i=0; i<ATTACHList.count; i++) {
        NSMutableDictionary *dic = [ATTACHList objectAtIndex:i];
        [attachXML appendString:@"<ATTACHInfo>"];
        [attachXML appendString:@"<AttachGuid>"];
        [attachXML appendString:[dic objectForKey:@"fileguid"]];
        [attachXML appendString:@"</AttachGuid>"];
        [attachXML appendString:@"<AttachName>"];
        [attachXML appendString:[dic objectForKey:@"filename"]];
        [attachXML appendString:@"</AttachName>"];
        [attachXML appendString:@"<FileContent>"];
        if ([[dic objectForKey:@"type"] isEqualToString:@"1"]) {
            
            NSData *data = [[NSFileManager defaultManager] contentsAtPath:[dic objectForKey:@"filepath"]];
            NSString *datastr = [GTMBase64 stringByEncodingData:data];
            [attachXML appendString:datastr];
        }
        [attachXML appendString:@"</FileContent>"];
        [attachXML appendString:@"</ATTACHInfo>"];
        
    }
    [attachXML appendString:@"</ATTACHList>"];
    
    WebServiceHelper *webserviceHelper = [[WebServiceHelper alloc]initWebServiceWithUrl:url wsNamespace:wsNamespace method:@"Mail_Send"];
    NSString *ParasXml = @"<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><UserGuid>%@</UserGuid><Subject>%@</Subject><ToUserGuidList>%@</ToUserGuidList><SecretUserGuidList>%@</SecretUserGuidList><ChaoSongUserGuidList>%@</ChaoSongUserGuidList><MailContent>%@</MailContent><IFNotice>%@</IFNotice>%@</paras>";
    ParasXml = [NSString stringWithFormat:ParasXml,userguid,Subject,ToUserGuidList,SecretUserGuidList,ChaoSongUserGuidList,MailContent,IFNotice,attachXML];
    ParasXml = [StringUtil filterXmlString:ParasXml];
    [webserviceHelper addParameterForString:@"ValidateData" value:ValidateData];
    [webserviceHelper addParameterForString:@"ParasXml" value:ParasXml];
    NSString *bs = webserviceHelper.start;
    [self sendDataRefreshUI:bs];
}

@end
